package com.yanqu.road.server.manger.config.grandchild.talent.trigger;

import com.yanqu.road.entity.config.grandchild.GrandChildTalentInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResult;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResultBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParamBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillResult;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffect;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffectStage;
import com.yanqu.road.server.manger.config.grandchild.talent.utils.TalentUtils;

import java.util.ArrayList;
import java.util.List;

public class TalentTriggerTTQ implements ITalentTrigger {

    @Override
    public TalentTriggerResult getResult(TalentTriggerParam param) {
        List<TalentTriggerSkillResult> skillResultList = new ArrayList<>();

        List<Integer> talentList;
        if (param.getStage() == eGrandChildSkillEffectStage.CHOICE_TALENT) {
            talentList = param.getChosenTalentList();
        } else {
            talentList = param.getGrandChild().getTalentList();
        }

        int count = 0;
        for (int talentId : talentList) {
            GrandChildTalentInfo talentInfo = GrandChildConfigMgr.getGrandChildTalentInfo(talentId);
            int value1 = talentInfo.getQuality();
            int value2 = param.getCondition().getValue();
            if (param.getCondition().getFlag().getResult(value1, value2)) {
                count++;
            }
        }

        List<SkillInfo> skillInfo = TalentUtils.getSkillInfo(param.getTalentInfo().getSkills(), param.getStage());
        for (SkillInfo info : skillInfo) {
            TalentTriggerSkillParam skillParam = TalentTriggerSkillParamBuilder.aTalentTriggerSkillParam()
                    .setGrandChild(param.getGrandChild())
                    .setSkillInfo(info)
                    .setLastTriggerList(param.getLastTriggerList())
                    .build();

            for (int i = 0; i < count; i++) {
                TalentTriggerSkillResult result = eGrandChildSkillEffect.getResult(skillParam);
                skillResultList.add(result);
            }
        }

        return TalentTriggerResultBuilder.aTalentTriggerResult()
                .setTalentId(param.getTalentInfo().getId())
                .setSkillResultList(skillResultList)
                .build();
    }
}
